package mystack.impl;

import mystack.MyStacK;

import java.lang.reflect.Array;

/**
 * Created by Administrator on 2017/3/16.
 */
public class MyStackimpl<E>  implements MyStacK<E>{
    private int top; //指示顺序栈的栈顶
    private E []data;//，数组，用于存储顺序栈中的数据元素
    private int maxsize; //顺序栈的容量

    public MyStackimpl(Class<E> type,int size) {
        data= (E[]) Array.newInstance(type,size);
        maxsize=size;
        top=-1;
    }

    //    入栈操作
    @Override
    public E push(E item) {
        if (!isFull()){
            data[++top]=item;
        }
        return item;
    }
//出栈操作
    @Override
    public E pop() {
        E item=null;
        if (!isFull()){
           item=data[top--];
        }
        return item;
    }
//获取栈的元素
    @Override
    public E peek() {
        E item=null;
        if (!isEmpty()){
            item=data[top];
        }
        return item;
    }
//求栈的长度
    @Override
    public int size() {
        return top++;
    }
//判断顺序栈是否为空
    @Override
    public boolean isEmpty() {
        if (top==-1){
            return true;
        }else {
            return false;
        }
    }
//判断顺序栈是否为满
    @Override
    public boolean isFull() {
        if (top==maxsize-1){
            return true;
        }else {
            return false;
        }


    }
}
